#import "Player.h"
#import "CharInfo.h"
#import "Sprite.h"
@implementation Player
@synthesize info;
@synthesize icon;
@synthesize  head, face,body,legs,feets,element,wapon,rArm,lArm;

- (bool) isEnemy {
	return false;
}

- (void) changeMood {
	switch ( [info state]) {
		case STATE_SHOWING:
			[face setCurrentFrame:2];
			[lArm setCurrentFrame:1];
			[rArm setCurrentFrame:1];
			break;
		case STATE_WINNING:
			[face setCurrentFrame:3];
			[lArm setCurrentFrame:2];
			[rArm setCurrentFrame:2];
			break;
		case STATE_LOSSING: 
			[face setCurrentFrame:1];
			[lArm setCurrentFrame:0];
			[rArm setCurrentFrame:0];
			break;
		case STATE_DYING:
			[face setCurrentFrame:0];
			[lArm setCurrentFrame:2];
			[rArm setCurrentFrame:2];
			break;
		case STATE_LEVELING:
			[face setCurrentFrame:4];
			[lArm setCurrentFrame:2];
			[rArm setCurrentFrame:2];
			break;
		case STATE_DRAWING:
			[face setCurrentFrame:2];
			[lArm setCurrentFrame:1];
			[rArm setCurrentFrame:1];
			break;
		case STATE_NONE:
			[face setCurrentFrame:2];
			[lArm setCurrentFrame:0];
			[rArm setCurrentFrame:0];
			break;
		case STATE_IDLE:
		default:
			[passiveArm setCurrentFrame:1];
			if([activeArm getCurrentFrame]==1)
				[activeArm setCurrentFrame:2];
			else
				[activeArm setCurrentFrame:1];
			[face setCurrentFrame:2];
			break;
	}
}

-(void) changePos {
	
	currentPos = (currentPos + 1) % 8;
	switch (currentPos % 8) {
		case 0:
			x = 0;
			y = 1;
			break;
		case 1:
			x = 1;
			y = 0;
			break;
		case 2:
			x = 1;
			y = 0;
			break;
		case 3:
			x = 1;
			y = -1;
			break;
		case 4:
			x = 0;
			y = 1;
			break;
		case 5:
			x = -1;
			y = 0;
			break;
		case 6:
			x = -1;
			y = 0;
			break;
			
		case 7:
			x = -1;
			y = -1;
			break;
	}
	[head move:x y:y];
	[legs move:x y:y];
	[body move:x y:-y];
	[skill move:x y:-y];
	[face move:x y:y];
	[lArm move:x y:-y];
	[rArm move:x y:-y];
	[fist move:x y:-y];
}

- (void) drawImage {
	[head paint];
	[face paint];
	[legs paint];
	[rArm paint];
	[body paint];
	[lArm paint];
	[feets paint];
	[skill paint];
	if ( info.state == STATE_SHOWING && info.command >= 0) {
		[fist paint];
		// wapon paint(g];
		// element paint(g];
		// icon paint(g];
	}
}





- (id) init:(CharInfo*) c {
	if([super init]){
		p2 = [self isEnemy];
		
		self.info = c;
		[info retain];		
		info.state=STATE_IDLE;
		info.command=COMMAND_UNKNOWN;
		UIImage* i=	[UIImage imageNamed:c.head] ;
		head = [[Sprite alloc]initWithImage:i mirror:p2];
		NSArray* fa =[NSArray arrayWithObjects: [UIImage imageNamed:c.face0],
					  [UIImage imageNamed:c.face1],[UIImage imageNamed:c.face2],
					  [UIImage imageNamed:c.face3],[UIImage imageNamed:c.face4],nil];
		
		face = [[Sprite alloc] initWithArray:fa mirror:p2];
		body = [[Sprite alloc]initWithImage:[UIImage imageNamed:c.body] mirror:p2];
		
		// cache handles to our key sprites & other drawables
        fa=[NSArray arrayWithObjects:[UIImage imageNamed:c.hand1],[UIImage imageNamed:c.hand2],[UIImage imageNamed:c.hand3],nil];
        lFist = [[Sprite alloc] initWithArray:fa  mirror:p2];
        fa=[NSArray arrayWithObjects:[UIImage imageNamed:c.hand4],[UIImage imageNamed:c.hand5],[UIImage imageNamed:c.hand6],nil];
        rFist = [[Sprite alloc] initWithArray:fa  mirror:p2];
		fa=[NSArray arrayWithObjects:[UIImage imageNamed:c.arm1],[UIImage imageNamed:c.arm2],[UIImage imageNamed:c.arm3],nil];
		lArm = [[Sprite alloc] initWithArray:fa mirror:p2];
		fa=[NSArray arrayWithObjects:[UIImage imageNamed:c.arm4],[UIImage imageNamed:c.arm5],[UIImage imageNamed:c.arm6],nil];
		rArm = [[Sprite alloc] initWithArray:fa mirror:p2];
		legs = [[Sprite alloc] initWithImage:[UIImage imageNamed:c.legs] mirror:p2];
		feets = [[Sprite alloc]initWithImage:[UIImage imageNamed:c.feets] mirror:p2];
		icon = [[Sprite alloc]initWithImage:[UIImage imageNamed:c.icon] mirror:p2];
		element = [[Sprite alloc]initWithImage:[UIImage imageNamed:c.element] mirror:p2];
		skill = [[Sprite alloc]initWithImage:[UIImage imageNamed:c.job] mirror:p2];
		wapon = [[Sprite alloc]initWithImage:[UIImage imageNamed:c.wapon] mirror:p2];
	}
	[self initPosition];
	return self;
}

- (void) initPosition {
	x=0;
	y=80;
	int y2=240;
	[head setPosition:x y:y];
	[face setPosition:x+50	 y:y+50];
	[body setPosition:x	 y:y2];
	[legs setPosition:x	 y:y2];
	[feets setPosition:x	 y:y2+120];
	[lArm setPosition:x	 y:y2];
	[rArm setPosition:x+80	 y:y2];
	[lFist setPosition:x	 y:y2];
	[rFist setPosition:x+100	 y:y2];
	[icon setPosition:2	y:2];
	[ element setPosition:x+80 y:y2];
	[ wapon setPosition:x y:y2];
	[ skill setPosition:x y:y2];
	activeArm = lArm;
    fist=lFist;   
	passiveArm=rArm;
}

- (void) paint {
	[self changePos];
	[self changeMood ];
	[self drawImage];
}

- (void) setCommand:(int) currentCommand {
	[fist setCurrentFrame:(currentCommand % 3)];
	info.command=currentCommand;
}

- (void) move:(int) dx y:(int)dy{
	[head move:dx y:dy];
	[legs move :dx y:dy];
    [ body move :dx y:dy];
	[ face move :dx y:dy];
	[ lArm move :dx y:dy];
	[ rArm move :dx y:dy];
	[ fist move :dx y:dy];
	[ feets move :dx y:dy];
}

@end
